#region "ObjectExtensions"

/*
 * ####     Developer Name : Wang Yucai
 * 
 * ####     Development Tool : Microsoft VisualStudio 2010 Ultimate Edition
 * 
 * ####     Create Time : 2014/7/8 11:00:58
 * 
 * ####     Namespace : System
 * 
 * ####     Type Name : ObjectExtensions
 * 
 * ####     Full Name : System.ObjectExtensions
 * 
 * ####     Machine Name : GLCHQWYCWINW7
 * 
 * ####     UserName : GUOCOLAND/wangyucai
 * 
 * ####     CLR Version : 4.0.30319.18408
 * 
 * ####     Target Framework Version : 4.0
 */

#endregion

using System.Diagnostics;
using Masterduner.SourceStudio.Practices.DevelopmentLibrary;

namespace System
{
    /// <summary>
    /// <para>System.ObjectExtensions</para>
    /// <para>
    /// 为<see cref="Object"/>提供的扩展方法。
    /// </para>
    /// </summary>
    /// <remarks>
    /// <para>Target Framework Version : 4.0</para>
    /// </remarks>
    public static class ObjectExtensions
    {
        #region IsNull
        /// <summary>
        /// 验证<paramref name="current"/>是否为空指针。
        /// </summary>
        /// <param name="current">当前的<see cref="Object"/>对象实例。</param>
        /// <returns>如果<paramref name="current"/>为空指针则返回true；否则返回false。</returns>
        static public bool IsNull(this object current)
        {
            bool result = object.ReferenceEquals(current, null);
            Debug.WriteLine("ObjectExtensions::IsNull::current::is {0}", result ? "null" : "not null");
            return result;
        }
        #endregion

        #region NotIsNull
        /// <summary>
        /// 验证<paramref name="current"/>是否不为空指针。
        /// </summary>
        /// <param name="current">当前的<see cref="Object"/>对象实例。</param>
        /// <returns>如果<paramref name="current"/>不为空指针则返回true；否则返回false。</returns>
        static public bool NotIsNull(this object current)
        {
            return !current.IsNull();
        }
        #endregion

        #region Is
        /// <summary>
        /// 验证<paramref name="current"/>是否继承自<typeparamref name="TBase"/>。
        /// </summary>
        /// <typeparam name="TBase">基类或接口类型。</typeparam>
        /// <param name="current"><see cref="Object"/>对象实例。</param>
        /// <returns>如果<paramref name="current"/>继承自<typeparamref name="TBase"/>则返回true；否则返回false。</returns>
        /// <remarks>
        /// 如果<paramref name="current"/>为空指针，则返回true。
        /// </remarks>
        static public bool Is<TBase>(this object current)
        {
            return current.IsNull()
                ? true
                : current is TBase;
        }
        #endregion

        #region ForceTo
        /// <summary>
        /// 将<paramref name="current"/>强制转换为<typeparamref name="T"/>类型。
        /// </summary>
        /// <typeparam name="T">目标类型。</typeparam>
        /// <param name="current"><see cref="Object"/>对象实例。</param>
        /// <returns><typeparamref name="T"/>类型值。</returns>
        /// <remarks>如果<paramref name="current"/>为空指针则返回<typeparamref name="T"/>类型的默认值。</remarks>
        static public T ForceTo<T>(this object current)
        {
            if (current.NotIsNull())
            {
                if (!current.Is<T>()) throw new ArgumentsExceptionWrapper();
                return (T)current;
            }
            else
            {
                return TExpando<T>.GetDefault();
            }
        }
        #endregion
    }
}

#region README

/*
 * ####     ©2014 Wang Yucai
 * 
 * ####     Contact me
 *                  
 *                  E-mail : Masterduner@Yeah.net
 *                  Mobile : 13621384677
 *                  QQ : 180261899
 */

#endregion